package com.example.practicetest.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.practicetest.service.GoodsService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author wushuang
 * 用于商品goods的控制器层
 */
@WebServlet(name = "goods", value = "/goods/*")
public class GoodsController extends HttpServlet {

    GoodsService goodsService = new GoodsService();

    /**
     * 判断是否登录
     * @param req
     * @param resp
     * @return
     */
    public boolean isLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if(req.getSession().getAttribute("user") == null){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 0);
            jsonObject.put("msg", "请先登录");
            jsonObject.put("data", null);
            resp.getWriter().write(jsonObject.toJSONString());
            return false;
        }else{
            return true;
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String uri = req.getRequestURI();
        if(!isLogin(req,resp)){
            return;
        }
        switch (uri){
            case "/goods/search"-> doSearch(req,resp);
            default->{

            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String uri = req.getRequestURI();
        if(!isLogin(req,resp)){
            return;
        }
        switch (uri){
            case "/goods/del"-> doDel(req,resp);
            case "/goods/purchase"-> doPurchase(req,resp);
            case "/goods/insert"-> doInsert(req,resp);
            default->{

            }
        }
    }

    /**
     * 根据商品名称name搜索商品
     * @param req
     * @param resp
     * @throws IOException
     */
    private void doSearch(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String name = req.getParameter("name");
        JSONObject jsonObject = new JSONObject();
        if(name == null || "".equals(name.trim())){
            jsonObject.put("code",0);
            jsonObject.put("msg","请输入商品名称");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
        } else if(goodsService.findGoodsByName(name) == null){
            jsonObject.put("code",0);
            jsonObject.put("msg","未找到该商品");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
        }else{
            jsonObject.put("code",0);
            jsonObject.put("msg","检索成功");
            jsonObject.put("data",goodsService.findGoodsByName(name));
            resp.getWriter().write(jsonObject.toJSONString());
        }
    }

    /**
     * 根据number【商品编号】删除商品
     * @param req
     * @param resp
     * @throws IOException
     */
    private void doDel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if(!isLogin(req,resp)){
            return;
        }
        String number = req.getParameter("number");
        JSONObject jsonObject = new JSONObject();
        if(number == null || "".equals(number.trim())){
            jsonObject.put("code",0);
            jsonObject.put("msg","请输入商品名称");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
            return ;
        }
        if(goodsService.findGoods(number) == null){
            jsonObject.put("code",0);
            jsonObject.put("msg","未找到该商品");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
            return ;
        }
        if(goodsService.deleteGoods(number) == 1){
            jsonObject.put("code",0);
            jsonObject.put("msg","删除成功");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
        }else{
            jsonObject.put("code",0);
            jsonObject.put("msg","删除失败");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
        }
    }

    /**
     * 根据number【商品编号】更新库存，库存-1
     * @param req
     * @param resp
     * @throws IOException
     */
    private void doPurchase(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if(!isLogin(req,resp)){
            return;
        }
        if(goodsService.purchaseGoods(req.getParameter("number")) == 1){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",0);
            jsonObject.put("msg","更新成功");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
        }else{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",0);
            jsonObject.put("msg","更新失败");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
        }
    }

    /**
     * 把csv文件里的内容插入到数据库里
     * @param req
     * @param resp
     * @throws IOException
     */
    private void doInsert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if(!isLogin(req,resp)){
            return;
        }
        if(goodsService.getGoodsFromCsv()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",0);
            jsonObject.put("msg","添加成功");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
        }else{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code",0);
            jsonObject.put("msg","添加失败");
            jsonObject.put("data",null);
            resp.getWriter().write(jsonObject.toJSONString());
        }
    }

}
